package com.beagle.njddapp.weight.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.amap.api.services.core.PoiItem;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.beagle.njddapp.R;
import com.beagle.njddapp.bean.owner.NjBean;
import com.beagle.njddapp.core.LocationManager;
import com.beagle.njddapp.core.UserService;
import com.beagle.njddapp.utils.Utils;
import com.blankj.utilcode.util.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author hxx
 * @date 2021-03-13 20:13
 */
public class JiayouDIalog extends Dialog {

    @BindView(R.id.jiayou_nbumber)
    TextView tvNumber;
    @BindView(R.id.jiayou_phone)
    TextView tvPhone;
    @BindView(R.id.jiayou_count)
    EditText tvCount;
    @BindView(R.id.jiayou_address)
    TextView tvAddress;
    @BindView(R.id.view)
    View view;
    @BindView(R.id.ed_address)
    EditText edAddress;

    private NjBean njBean;
    private JiaYouDialogHandleCallback jiaYouDialogHandleCallback;

    public void setJiaYouDialogHandleCallback(JiaYouDialogHandleCallback jiaYouDialogHandleCallback) {
        this.jiaYouDialogHandleCallback = jiaYouDialogHandleCallback;
    }

    public void setNjBean(NjBean njBean) {
        this.njBean = njBean;
//        GeocodeSearch geocoderSearch = new GeocodeSearch(getContext());
//        geocoderSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener(){
//            @Override
//            public void onGeocodeSearched(GeocodeResult result, int rCode) {
//            }
//            @Override
//            public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
//                try {
//                    tvAddress.setText(result.getRegeocodeAddress().getFormatAddress());
//                }catch (Exception e){
//                }
//            }});
//        com.amap.api.services.core.LatLonPoint lp = new  com.amap.api.services.core.LatLonPoint(njBean.getLatitude(),njBean.getLongitude());
//        RegeocodeQuery query = new RegeocodeQuery(lp,200,GeocodeSearch.AMAP);
//        geocoderSearch.getFromLocationAsyn(query);
    }

    public JiayouDIalog(@NonNull Context context) {
        super(context);
    }

    @Override
    public void show() {
        super.show();
        tvCount.setText("");
        tvAddress.setText("");
        edAddress.setText("");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_jiayou);
        ButterKnife.bind(this);
        if (getWindow() != null) {
            //按空白处不能取消动画
            setCanceledOnTouchOutside(true);
            //设置window背景，默认的背景会有Padding值，不能全屏。当然不一定要是透明，你可以设置其他背景，替换默认的背景即可。
            getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            //一定要在setContentView之后调用，否则无效
            getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            getWindow().setWindowAnimations(R.style.DialogAnim);
        }
        tvPhone.setText(UserService.getInstance().getPhone());
        tvCount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().contains(".")){
                    if (s.length()-1-s.toString().indexOf(".")>3){
                        s=s.toString().subSequence(0,s.toString().indexOf(".")+4);
                        tvCount.setText(s);
                        tvCount.setSelection(s.length());
                    }
                }

                if(s.toString().trim().substring(0).equals(".")){
                    s ="0"+s;
                    tvCount.setText(s);
                    tvCount.setSelection(2);
                }

                if(s.toString().startsWith("0")
                        && s.toString().trim().length()>4){
                    if(!s.toString().substring(1,3).equals(".")){
                        tvCount.setText(s.subSequence(0,4));
                        tvCount.setSelection(3);
                        return;
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }

        });
        tvAddress.setHint("请选择位置");
        view.setVisibility(View.GONE);
        edAddress.setVisibility(View.GONE);
    }
    public void dirverUpdate(){
        tvAddress.setHint("请选择位置");
        view.setVisibility(View.GONE);
        edAddress.setVisibility(View.GONE);
//        tvAddress.setText(LocationManager.getInstance().getLocation());
    }
    public void dirverUpdate(PoiItem poiItem){
        tvAddress.setText(poiItem.getProvinceName()+poiItem.getCityName()+poiItem.getAdName()+poiItem.getTitle()+poiItem.getSnippet());
        edAddress.setVisibility(View.VISIBLE);
        edAddress.setText("");
        view.setVisibility(View.VISIBLE);
    }
    public void dirverUpdateLocation(){
        tvAddress.setText(LocationManager.getInstance().getLocation());
        edAddress.setVisibility(View.VISIBLE);
        edAddress.setText("");
        view.setVisibility(View.VISIBLE);
    }
    @OnClick({R.id.tv_cancle,R.id.tv_ok,R.id.jiayou_address})
    public void onCLick(View view){
        switch (view.getId()){
            case R.id.jiayou_address:
                jiaYouDialogHandleCallback.chooseAera();
                break;
            case R.id.tv_cancle:
                dismiss();
                break;
            case R.id.tv_ok:
                if (TextUtils.isEmpty(tvCount.getText().toString())||TextUtils.isEmpty(tvPhone.getText().toString())||TextUtils.isEmpty(tvAddress.getText().toString())){
                    ToastUtils.showShort("请填写加油量或加油位置");
                    return;
                }
                jiaYouDialogHandleCallback.JiaYouOk(njBean,tvPhone.getText().toString(),tvAddress.getText().toString()+edAddress.getText().toString(),tvCount.getText().toString());
                dismiss();
                break;
        }
    }
    public interface  JiaYouDialogHandleCallback{
        void JiaYouOk(NjBean njBean,String phone,String address,String count);
        void chooseAera();
    }
}
